<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Illuminate\Validation\Rules\Password;
use Modules\Admin\Entities\Admin;
use Modules\Personnel\Entities\Teacher;

class ResetPwd extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'resetpwd {model : 模型名称} {--w|where= : where条件字段-值"id=1"} {--p|password= : 密码}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '修改密码
                                密码必须包含 大小写数字特殊符号  并且不能小于6位
                                使用方法:  php artisan resetpwd admin -w id=1 -p 123';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $model = $this->argument('model');
        $password = $this->option('password');
        $where = $this->option('where');
        if (Str::contains($where, '=')) {
            [$field, $value] = explode("=", $where);
            $operator = '=';
        }
        if (Str::contains($where, '>')) {
            [$field, $value] = explode(">", $where);
            $operator = '>';
        }
        if (Str::contains($where, '<')) {
            [$field, $value] = explode("<", $where);
            $operator = '<';
        }
        if (Str::contains($where, '>=')) {
            [$field, $value] = explode(">=", $where);
            $operator = '>=';
        }
        if (Str::contains($where, '<=')) {
            [$field, $value] = explode("<=", $where);
            $operator = '<=';
        }
        if (Str::contains($where, '<>')) {
            [$field, $value] = explode("<>", $where);
            $operator = '<>';
        }
        if (Str::contains($where, '!=')) {
            [$field, $value] = explode("!=", $where);
            $operator = '!=';
        }
        $value = $value == 'null' ? null : $value;
        switch ($model) {
            case "admin":
                Admin::where($field, $operator, $value)->update([
                    'password' => \Hash::make($password)
                ]);
                break;
            case "teacher":
                Teacher::where($field, $operator, $value)->update([
                    'password' => \Hash::make($password)
                ]);
                break;
        }
    }
}
